package com.zky.gulimall.order.web;

import com.zky.common.exception.NoStockException;
import com.zky.common.utils.PageUtils;
import com.zky.common.utils.R;
import com.zky.gulimall.order.service.OrderService;
import com.zky.gulimall.order.vo.OrderConfirmVo;
import com.zky.gulimall.order.vo.OrderSubmitResponseVo;
import com.zky.gulimall.order.vo.OrderSubmitVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @Description 订单controller
 * @Author 张开源
 * @Date 2022/12/9 11:34
 * @Version 1.0
 **/
@Controller
@Slf4j
public class OrderWebController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo confirmVo = orderService.confirmOrder();
        model.addAttribute("orderConfirmData", confirmVo);
        return "confirm";
    }

    /**
     * 创建订单
     *
     * @param submitVo
     * @return
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo submitVo, Model model, RedirectAttributes attributes) {
        try {
            log.warn("submitVo:" + submitVo);
            OrderSubmitResponseVo responseVo = orderService.submitOrder(submitVo);
            log.warn("responseVo:" + responseVo);
            if (responseVo != null && responseVo.getCode() == 0) {
                //下单成功
                model.addAttribute("submitOrderResp", responseVo);
                return "pay";
            }
            String msg = "下单失败";
            if (responseVo != null) {
                switch (responseVo.getCode()) {
                    case 1:
                        msg += "订单信息过期，请刷新重新提交";
                    case 2:
                        msg += "订单商品价格发生变化，请确认后再次提交";
                    case 3:
                        msg += "库存锁定失败,商品库存不足";
                }
            }
            attributes.addFlashAttribute("msg", msg);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof NoStockException) {
                String message = ((NoStockException) e).getMessage();
                attributes.addFlashAttribute("msg", message);
            }
        }
        //下单失败，返回原来的下单确认页
        return "redirect:http://order.zky.com/toTrade";
    }

    /**
     * 列表
     */
    @ResponseBody
    @GetMapping("/listWithItem")
    public R listWithItem(@RequestParam Map<String, Object> params) {
        PageUtils page = orderService.queryPageWithItem(params);
        return R.ok().put("page", page);
    }
}
